package com.ruoyi.mall.front.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.mall.entity.MallCoupons;
import com.ruoyi.mall.entity.MallPackages;
import com.ruoyi.mall.front.service.FrontLoginService;
import com.ruoyi.mall.front.vo.MallMembersPackageVo;
import com.ruoyi.mall.front.vo.MallMembersVo;
import com.ruoyi.mall.service.IMallCouponsService;
import com.ruoyi.mall.service.IMallPackagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName FrontLoginController
 * @Description: 前端优惠券控制器
 * @Author cjc
 * @Date 2020/5/15
 * @Version V1.0
 **/

@RestController
@RequestMapping("mall/front")
public class FrontPackageController extends BaseController {
    /**
     * 前端业务
     */
    @Autowired
    private IMallPackagesService mallPackagesService;
    /**
     * 获取所有优惠包
     */
    @Autowired
    private IMallCouponsService mallCouponsService;
    @GetMapping("packages")
    public AjaxResult packages(){
        return AjaxResult.success(mallPackagesService.selectMallPackagesList(null));
    }
    /**
     * 根据Id获取d优惠包
     */
    @GetMapping("packages/{packageId}")
    public AjaxResult getByPackageId(@PathVariable("packageId") Long packageId){
        return AjaxResult.success(mallPackagesService.selectMallPackagesById(packageId));
    }
    /**
     * 根据优惠包id获取优惠劵
     */
    @GetMapping("coupons/{packageId}")
    public AjaxResult getCouponsByPackageId(@PathVariable("packageId") Long packageId){
        return AjaxResult.success(mallCouponsService.selectMallCouponsByPackageId(packageId));
    }
    /**
     * 根据优惠包id获取优惠劵
     */
    @GetMapping("coupons/id/{couponsId}")
    public AjaxResult getCouponsById(@PathVariable("couponsId") Long couponsId){
        return AjaxResult.success(mallCouponsService.selectMallCouponsById(couponsId));
    }
    /**
     * 购买优惠包
     */
    @PostMapping("sellPackage")
    public AjaxResult sellPackage(@RequestBody MallMembersPackageVo membersPackageVo){
        if(null==membersPackageVo.getMembersId()||null == membersPackageVo.getPackageId())
        {
            return AjaxResult.error("没有该会员或该优惠包");

        }
        return AjaxResult.success(mallPackagesService.sellPackage(membersPackageVo.getPackageId(),membersPackageVo.getMembersId()));
    }
}
